package com.rao.travel.service.impl;

import com.rao.travel.dao.RoleDao;
import com.rao.travel.pojo.Permission;
import com.rao.travel.pojo.Role;
import com.rao.travel.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className RoleServiceImpl
 * @date 2019/11/5 10:01
 * @package com.rao.travel.service.impl
 * @description
 */

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /*查询所有角色*/
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    /*保存角色*/
    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    /*查询角色详情*/
    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    /*删除角色*/
    @Override
    public void deleteById(String id) {
        /*要从三张表中都删除*/
        /*从users_role中删除*/
        roleDao.deleteFromUsersRole(id);
        /*从role_permission中删除*/
        roleDao.deleteFromRolePermission(id);
        /*从role中删除*/
        roleDao.deleteFromRole(id);
    }

    /*查询该角色还未拥有的权限*/
    @Override
    public List<Permission> findOtherPermission(String id) {
        return roleDao.findOtherPermission(id);
    }

    /*为角色添加权限*/
    @Override
    public void addPermissionToRole(String roleId, String[] ids) {
        for (String permissionId : ids){
            roleDao.addPermissionToRole(permissionId, roleId);
        }
    }
}
